import MathJax from './MathJax.js'
//样例
let exampleItem=[
    {
        sets:"{1,2,3,4,5,6,7}",
        relations:"{(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(1,2),(2,1),(1,6),(6,1),(1,4),(4,1),(2,4),(4,2),(2,6),(6,2),(3,4),(4,3),(3,6),(6,3),(4,5),(5,4),(4,6),(6,4)}"
    },
    {
        sets:"{1,2,3,4}",
        relations:"{(1,1),(2,2),(3,3),(4,4),(1,2),(1,4),(4,1),(1,2),(2,1),(3,2),(2,3)}"
    },
    {
        sets:"{1,2,3,4,5}",
        relations:"{(1,1),(2,2),(3,3),(4,4),(5,5),(1,3),(3,1),(2,3),(3,2),(2,4),(4,2),(3,4),(4,3),(2,5),(5,2),(3,5),(5,3),(4,5),(5,4)}"
    }
]
let matrix=[];//关系矩阵
var app=new Vue({
    el:"#app",
    data:{
        sets:"",
        relations:"",
        matrixs:'',
        isShowMatrix:false,
        condition1:'',
        condition2:'',
        condition3:'',
        result:'',
        setList:[],
        quotientSet:"",
        isShowResult:false,
        examples:[1,2,3]
    },
    methods: {
        relationShow(){
            matrix=[];
            //字符串处理
            console.log(this.sets);
            console.log(this.relations);
            let sets=[],relations=[];
            //检查格式是否正确
            try{
                var reg = RegExp(/^{[a-zA-Z0-9]+(,([a-zA-Z0-9]+))+}$/);
                if(!this.sets.match(reg)){
                    modalApp.modalAlert("集合格式输入不正确");return;
                }
                var reg1=RegExp(/^{\([a-zA-Z0-9]+,[a-zA-Z0-9]+\)(,\([a-zA-Z0-9]+,[a-zA-Z0-9]+\))+}$/);
                if(!this.relations.match(reg1)){
                    modalApp.modalAlert("关系格式输入不正确");return;
                }
                sets=this.sets.slice(1,this.sets.length-1).split(',');
                console.log(sets);
                let rel=this.relations.slice(1,this.relations.length-1).split(',');
                for(let i=0;i<rel.length;i=i+2){
                    relations.push({
                        a1:rel[i].slice(1),
                        a2:rel[i+1].slice(0,rel[i+1].length-1),
                    });
                }
                console.log(relations);
                //如果关系中的点在集合中不存在则 报错
                for(let i=0;i<sets.length;i++){
                    matrix[i]=[];
                    for(let j=0;j<sets.length;j++){
                        matrix[i][j]=0;
                    }
                }
                for(let i=0;i<relations.length;i++){
                    let t1=sets.indexOf(relations[i].a1);
                    let t2=sets.indexOf(relations[i].a2);
                    if(t1!=-1&&t2!=-1){
                        matrix[t1][t2]=1;
                    }
                    else{
                        modalApp.modalAlert("关系中的点在集合中不存在");return;
                    }
                }
                let num=0;
                //判断是否自反
                if(!selfReflexivity()){
                    this.condition1="不具有自反性"
                }
                else{
                    this.condition1="具有自反性";
                    num++;
                }
                //判断是否对称性
                if(!symmetry()){
                    this.condition2="不具有对称性"
                }
                else{
                    this.condition2="具有对称性";
                    num++;
                }
                if(num==2) this.result="是相容关系"
                else this.result="不是相容关系"
            }catch(e){
                console.log(e);
                return;
            }
        },
        formatMath() {
            let that = this;
            console.log(MathJax);
            that.$nextTick(function () {
                if(MathJax.isMathjaxConfig){//判断是否初始配置，若无则配置。
                    MathJax.initMathjaxConfig();
                }
                MathJax.MathQueue("matrix");//传入组件id，让组件被MathJax渲染
            })
        },
        equalityShow(){
            let relations=[],Cmatrix=[],sets=[];
            //检查格式是否正确
            try{
                var reg = RegExp(/^{[a-zA-Z0-9]+(,([a-zA-Z0-9]+))*}$/);
                if(!this.sets.match(reg)){
                    modalApp.modalAlert("集合格式输入不正确");return;
                }
                var reg1=RegExp(/^{\([a-zA-Z0-9]+,[a-zA-Z0-9]+\)(,\([a-zA-Z0-9]+,[a-zA-Z0-9]+\))*}$/);
                if(!this.relations.match(reg1)){
                    modalApp.modalAlert("关系格式输入不正确");return;
                }
                sets=this.sets.slice(1,this.sets.length-1).split(',');
                console.log(sets);
                let rel=this.relations.slice(1,this.relations.length-1).split(',');
                for(let i=0;i<rel.length;i=i+2){
                    relations.push({
                        a1:rel[i].slice(1),
                        a2:rel[i+1].slice(0,rel[i+1].length-1),
                    });
                }
                for(let i=0;i<sets.length;i++){
                    Cmatrix[i]=[];
                    for(let j=0;j<sets.length;j++){
                        Cmatrix[i][j]=0;
                    }
                }
                console.log(relations);

                for(let i=0;i<relations.length;i++){
                    let t1=sets.indexOf(relations[i].a1);
                    let t2=sets.indexOf(relations[i].a2);
                    if(t1!=-1&&t2!=-1){
                        Cmatrix[t1][t2]=1;
                    }
                    else{
                        modalApp.modalAlert("关系中的点在集合中不存在");return;
                    }
                }
                this.isShowResult=true;
                console.log(Cmatrix);
                let Xsets=[];
                //建立N个相容关系，每个相容关系中存放一个节点
                for(let i=0;i<sets.length;i++){
                    Xsets[i]=[];
                    Xsets[i].push(sets[i]);
                }
                console.log(Xsets);
                for(let i=0;i<sets.length;i++){
                    for(let j=0;j<sets.length;j++){
                        let t=Xsets[i].indexOf(sets[j])
                        if(t==-1){
                            //不在Xsets[i]中则判断是否加入后满足相容关系
                            let k=0;
                            for(k=0;k<Xsets[i].length;k++){
                                if(Cmatrix[j][sets.indexOf(Xsets[i][k])]!=1||Cmatrix[sets.indexOf(Xsets[i][k])][j]!=1) break;
                            }
                            if(k==Xsets[i].length) Xsets[i].push(sets[j]);
                        }
                    }
                    Xsets[i].sort((x,y)=>{
                        return x-y;
                    })
                }
                console.log(Xsets);
                //对重复集合进行处理
                let setList=[],quotientSet="{ ";
                for(let i=0;i<Xsets.length;i++){
                    if(setList.indexOf("{"+Xsets[i].toString()+"}")==-1){
                        setList.push("{"+Xsets[i].toString()+"}");
                        if(quotientSet=="{ ")
                        quotientSet+="{"+Xsets[i].toString()+"}";
                        else quotientSet+=",{"+Xsets[i].toString()+"}";
                    }
                }
                this.quotientSet=quotientSet+" }";
                this.setList=setList;
                console.log(setList);
                console.log(quotientSet);
            }catch(e){
                console.log(e);
                return;
            }
        },
        showExample(index){
            console.log(index);
            this.sets=exampleItem[index].sets;
            this.relations=exampleItem[index].relations;
        }
    },
})
var modalApp=new Vue({
    el:"#myModal1",
    data:{
        message:""
    },
    methods:{
        modalAlert(str){
            this.message=str;
            $('#myModal1').modal('toggle');
        }
    }
})

//自反性
function selfReflexivity(){
    for(let i=0;i<matrix.length;i++){
        if(matrix[i][i]!=1) return false
    }
    return true;
}
//对称性
function symmetry(){
     for(let i=0;i<matrix.length;i++){
        for(let j=i;j<matrix[i].length;j++){
            if(matrix[i][j]!=matrix[j][i]) return false
        }
    }
    return true;
}
//传递性
function Transitive(){
    //传递性
    let p = 0,x=matrix.length,y=matrix.length;
    for(let i = 0 ; i < x ; i++)
    {
        for(let j = 0 ; j < y ; j++)
        {
            if( 1 == matrix[i][j] )
            {
                for(let k = 0 ; k < y ; k++)
                {
                    if( 1 == matrix[j][k] && 1 != matrix[i][k] )
                    {
                        p = 1;
                        break;
                    }
                }
            }
        }
    }
    if(p) return false;
    else return true;
}